﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Kimd.Motion
{
    public class MultiAxisMoveStep : Step
    {
        private List<string> _actualPoint;
        public List<string> AxisNames { get; set; }
        public string PointName { get; set; }
        public MultiAxisMoveStep()
        {
            this.AxisNames = new List<string>();
            this._actualPoint = new List<string>();
        }
        public MultiAxisMoveStep(string description, StepType stepType)
            : base(description, stepType)
        {
        }
        public override void Prepare(Dictionary<string, Axis> machineAxis, Dictionary<string, Cylinder> machineCylinder, Tray tray = null, int trayIndex = -1, int nozzleIndex = -1)
        {
            _actualPoint = new List<string>();
            _actualPoint.Clear();
            AxisNames.ForEach(name =>
            {
                if (machineAxis[name].MotionPosition.ContainsKey(PointName))
                    _actualPoint.Add(PointName);
                else
                    _actualPoint.Add($"{PointName}-{trayIndex}-{nozzleIndex}");
            });
            AxisNames.ForEach(name => machineAxis[name].SetSpeed(SpeedType.Manual));
            base.Prepare(machineAxis, machineCylinder);
        }
        public override bool Run(Dictionary<string, Axis> machineAxis, Dictionary<string, Cylinder> machineCylinder, Dictionary<string, Sensor> machineSensor, out string errInfo)
        {
            List<string> stopReasons = new List<string>();
            List<string> stopReasons2 = new List<string>();
            for (int i = 0; i < AxisNames.Count; i++)
            {
                string err = "";
                machineAxis[AxisNames[i]].Move2Point(_actualPoint[i], out err, 0, false);
                if(!string.IsNullOrEmpty(err))
                    stopReasons.Add(err);
            }
            for (int i = 0; i < AxisNames.Count; i++)
            {
                string err = "";
                machineAxis[AxisNames[i]].Wait2Point(_actualPoint[i], 0, out err);
                if (!string.IsNullOrEmpty(err))
                    stopReasons2.Add(err);
            }
            errInfo = "";
            if (stopReasons.Count == 0 && stopReasons2.Count == 0)
                return true;
            else
            {
                errInfo += string.Join("\r\n", stopReasons);
                errInfo += "\r\n";
                errInfo += string.Join("\r\n", stopReasons2);
                return false;
            }
        }
        public override TreeNode ToTreeNode()
        {
            TreeNode node = new TreeNode();
            node.Text = this.ToString();
            node.Tag = this;
            return node;
        }
        public override void Stop(Dictionary<string, Axis> machineAxis)
        {
            AxisNames.ForEach(axis => machineAxis[axis].StopMovement = true);
        }

        public override string ToString()
        {
            return $"{this.Description}-[{string.Join(",", AxisNames)} 移动至 {PointName}]";
        }
    }
}
